home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-01-02 | 5.3 KB | 218 lines | [TEXT/MPS ] |
- PROGRAM rotString;
-
- {
- ••••••••••••••••••••••••••••••••••
- rotString by John D. Olsen
- for
- MacTutor Magazine
- © Jan 1988
- ••••••••••••••••••••••••••••••••••
- }
-
- USES
- {$LOAD MemQukOSToolPack}
- Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf;
- {$LOAD}
-
- CONST
- picDwgBeg = 130;
- picDwgEnd = 131;
- TextBegin = 150;
- TextEnd = 151;
- StringBegin = 152;
- StringEnd = 153;
- TextCenter = 154;
-
- TYPE
- TTxtPicRec = packed Record
- tJus: Byte;
- tFlip: Byte;
- tRot: integer;
- tLine: Byte;
- tCmnt: Byte;
- end;
- TTxtPicPtr = ^TTxtPicRec;
- TTxtPicHdl = ^TTxtPicPtr;
-
- TTxtCenter = Record
- y, x: Fixed;
- end;
- TTxtCenPtr = ^TTxtCenter;
- TTxtCenHdl = ^TTxtCenPtr;
-
- VAR
- thehandle : handle;
- windowRect, sourceRect, destRect : rect;
- myWindow : windowPtr;
- sourceMap, destMap, tempMap : BitMap;
- myLabelStr : str255;
-
-
- FUNCTION NewPtrClear( theSize: size ) : Ptr; EXTERNAL;
-
- PROCEDURE Rotate( srcMap, destMap : BitMap ); EXTERNAL;
-
- PROCEDURE NewBitMapClear( VAR theBitMap : BitMap );
- BEGIN
- WITH theBitMap, bounds DO
- BEGIN
- rowBytes := ((right - left + 15) DIV 16) * 2;
- baseAddr := NewPtrClear(rowBytes * (bottom - top));
- IF MemError <> noErr then baseAddr := NIL;
- END;
- END;{NewBitMapClear}
-
- PROCEDURE OpenWindow;
-
- CONST
- mBarHeightGlobal = $BAA;
-
- VAR
- screen : rect;
- mBarHeight : Integer;
- MemoryPtr : ^Integer;
-
- BEGIN
- MemoryPtr := Pointer( mBarHeightGlobal );
- mBarHeight := MemoryPtr^;
- screen := screenBits.bounds;
-
- SetRect( windowRect, screenBits.bounds.left + 5, screenBits.bounds.top
- + mBarHeight + 25,screenBits.bounds.right - 5,
- screenBits.bounds.bottom - 5 );
- myWindow := NewWindow( NIL, windowRect, 'Text Rotation' ,true , documentProc,
- windowPtr( -1 ), false, longint( 0 ));
- SetPort( myWindow );
-
- END;{OpenWindow}
-
-
- PROCEDURE xDrawString( myLabelStr: str255 );
-
- VAR
- myFontInfo: FontInfo;
- strWidth, mapLength, mapHeight, xOffset, yOffset : INTEGER;
- sourceMap, destMap : BitMap;
- sourceRect, destRect, myClipRect : Rect;
- MyRgn: RgnHandle;
- origPort, offScrGrafPort : GrafPtr;
- textHandle, centerHandle : Handle;
- Tx, Ty: fixed;
-
- BEGIN
- GetFontInfo(myFontInfo);
- strWidth := StringWidth(myLabelStr);
- mapLength := ((strWidth - 1) div 16 + 1) * 16;
- mapHeight := myFontInfo.ascent + 2 * ( myFontInfo.descent );
- SetRect(sourceRect, 0, 0, mapLength, mapHeight); { set up our offscreen bitMap }
- sourceMap.bounds := sourceRect;
- NewBitMapClear( sourceMap );
-
- GetPort(origPort); { create a new grafPort to make sure things stay clean }
- offScrGrafPort := GrafPtr(NewPtr(sizeof(GrafPort)));
- OpenPort(offScrGrafPort);
- offScrGrafPort^.portRect := sourceMap.bounds;
- SetPortBits(sourceMap);
- WITH offScrGrafPort^ DO
- BEGIN
- txFont := origPort^.txFont;
- txSize := origPort^.txSize;
- txFace := origPort^.txFace;
- END;
-
-
- GetFontInfo( myFontInfo ); { draw the string we want rotated into our temp bitMap }
- MoveTo( 0, myFontInfo.ascent);
- DrawString( myLabelStr );
-
- SetPort(origPort); { set our port back to the original }
- Rotate( sourceMap, destMap ); { rotate the offscreen bitmap }
-
- destRect := destMap.bounds; { where are we going to draw it ? }
- WITH destRect DO { offset to 10,10 no matter where we start }
- OffsetRect( destRect, -left, -top );
- OffsetRect( destRect, 10, 10 );
-
- {set up picComments }
- textHandle := NewHandle(sizeof(TTxtPicRec));
- centerHandle := NewHandle(sizeof(TTxtCenter));
- WITH TTxtPicHdl(textHandle)^^ DO
- BEGIN
- tJus := 2; {center}
- tFlip := 0; {no flip}
- tRot := 270; {rotate 270 degrees}
- tLine := 1; {single spacing}
- tCmnt := 0;
- END;
-
- HLock(centerHandle);
- WITH TTxtCenHdl(centerHandle)^^, destRect DO
- BEGIN
- Ty := FixRatio(bottom - top, 2);
- Tx := FixRatio(right - left, 2);
- xOffset := - strWidth div 2;
- yOffset := (myFontInfo.ascent - myFontInfo.descent) div 2;
- x := FixRatio(-xOffset, 1);
- y := FixRatio(-yOffset, 1);
- MoveTo(left + HiWrd(Tx) + xOffset, top + HiWrd(Ty) + yOffset);
- END;
-
- PicComment(picDwgBeg, 0, nil);
- PicComment(TextBegin, sizeof(TTxtPicRec), textHandle);
- PicComment(TextCenter, sizeof(TTxtCenter), centerHandle);
- HUnlock(centerHandle);
- DisposHandle(centerHandle);
- DisposHandle(textHandle);
-
-
- SetRect(myClipRect, 0, 0, 0, 0); { set myClipRect to 'nada' }
- ClipRect(myClipRect);
-
- { this just puts the string into the picture }
- DrawString(myLabelStr);
-
- { now set the clipRect back and draw the rotated bitmap }
- ClipRect(origPort^.portRect);
-
- CopyBits(destMap, origPort^.portBits, destMap.bounds, destRect, srcOr, nil);
-
- PicComment(TextEnd, 0, nil);
- PicComment(picDwgEnd, 0, nil);
-
- {clean up our mess}
- ClosePort(offScrGrafPort);
- DisposPtr(ptr(offScrGrafPort));
- DisposPtr(sourceMap.baseAddr);
- DisposPtr(destMap.baseAddr);
- SetClip(MyRgn);
- DisposeRgn(MyRgn);
- END;{xDrawString}
-
-
- { • • • • • • • • • • • • • • • M A I N • • • • • • • • • • • • • • • }
- BEGIN
- FlushEvents(everyEvent,0);
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- InitDialogs(NIL);
- InitCursor;
-
- OpenWindow;
-
- TextFont(Geneva);
- TextSize(12);
- TextFace([]);
-
- myLabelStr := 'Looking at text from a different angle';
-
- xDrawString( myLabelStr );
-
- REPEAT UNTIL button;
- DisposeWindow( myWindow );
- END.
-
-
-
-